using System;
using SCRUMPoker;
using SCRUMPoker.Web;


public class GamesListPresenter : PresenterBase<IGameListView>
{
    private ISession _Session;
    private IApp _App;

    public GamesListPresenter(IGameListView view, ISession session, IApp app) :
        base(view)
    {
        _Session = session;
        _App = app;
    }

    protected override void Init(bool isPostBack)
    {
        base.Init(isPostBack);

        View.CreateGame += delegate { CreateGame(); };
        View.DeleteGame += DeleteGame;
        View.GoToGame += GoToGame;
    }

    protected override void Load(bool isPostBack)
    {
        View.CurrentGames = _App.Games;

        base.Load(isPostBack);
    }

    private void CreateGame()
    {
        Game game = _App.CreateGame(View.GameSubject);
        game.AddParticipant(_Session.GameParticipant);
    }

    private void DeleteGame(object sender, EventArgs e)
    {
        _App.RemoveGame(sender.ToString());
    }

    private void GoToGame(object sender, EventArgs e)
    {
        _Session.CurrentGame = _App.FindGame(sender.ToString());
        _Session.CurrentGame.AddParticipant(_Session.GameParticipant);
    }
}